
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>开始 · Gitbook demo</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        <meta name="author" content="lanyue">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="intro.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <a target="_blank" href="https://github.com/vuejs/vuex/tree/1.0/docs">
            
                    
                    寻找 1.0 文档？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <a target="_blank" href="https://github.com/vuejs/vuex/releases">
            
                    
                    更新记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="installation.html">
            
                <a href="installation.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="intro.html">
            
                <a href="intro.html">
            
                    
                    Vuex 是什么？
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6" data-path="getting-started.html">
            
                <a href="getting-started.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    核心概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="state.html">
            
                <a href="state.html">
            
                    
                    State
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="getters.html">
            
                <a href="getters.html">
            
                    
                    Getters
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="mutations.html">
            
                <a href="mutations.html">
            
                    
                    Mutations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="actions.html">
            
                <a href="actions.html">
            
                    
                    Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    Modules
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="structure.html">
            
                <a href="structure.html">
            
                    
                    项目结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="plugins.html">
            
                <a href="plugins.html">
            
                    
                    插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="strict.html">
            
                <a href="strict.html">
            
                    
                    严格模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="forms.html">
            
                <a href="forms.html">
            
                    
                    表单处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="hot-reload.html">
            
                <a href="hot-reload.html">
            
                    
                    热重载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="api.html">
            
                <a href="api.html">
            
                    
                    API 文档
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >开始</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5F00;&#x59CB;">&#x5F00;&#x59CB;</h1>
<p>&#x6BCF;&#x4E00;&#x4E2A; Vuex &#x5E94;&#x7528;&#x7684;&#x6838;&#x5FC3;&#x5C31;&#x662F; store&#xFF08;&#x4ED3;&#x5E93;&#xFF09;&#x3002;&quot;store&quot; &#x57FA;&#x672C;&#x4E0A;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5BB9;&#x5668;&#xFF0C;&#x5B83;&#x5305;&#x542B;&#x7740;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x4E2D;&#x5927;&#x90E8;&#x5206;&#x7684;<strong>&#x72B6;&#x6001;(state)</strong>&#x3002;Vuex &#x548C;&#x5355;&#x7EAF;&#x7684;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x6709;&#x4EE5;&#x4E0B;&#x4E24;&#x70B9;&#x4E0D;&#x540C;&#xFF1A;</p>
<ol>
<li><p>Vuex &#x7684;&#x72B6;&#x6001;&#x5B58;&#x50A8;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x3002;&#x5F53; Vue &#x7EC4;&#x4EF6;&#x4ECE; store &#x4E2D;&#x8BFB;&#x53D6;&#x72B6;&#x6001;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x82E5; store &#x4E2D;&#x7684;&#x72B6;&#x6001;&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF0C;&#x90A3;&#x4E48;&#x76F8;&#x5E94;&#x7684;&#x7EC4;&#x4EF6;&#x4E5F;&#x4F1A;&#x76F8;&#x5E94;&#x5730;&#x5F97;&#x5230;&#x9AD8;&#x6548;&#x66F4;&#x65B0;&#x3002;</p>
</li>
<li><p>&#x4F60;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x6539;&#x53D8; store &#x4E2D;&#x7684;&#x72B6;&#x6001;&#x3002;&#x6539;&#x53D8; store &#x4E2D;&#x7684;&#x72B6;&#x6001;&#x7684;&#x552F;&#x4E00;&#x9014;&#x5F84;&#x5C31;&#x662F;&#x663E;&#x5F0F;&#x5730;<strong>&#x63D0;&#x4EA4;(commit) mutations</strong>&#x3002;&#x8FD9;&#x6837;&#x4F7F;&#x5F97;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x5730;&#x8DDF;&#x8E2A;&#x6BCF;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x4ECE;&#x800C;&#x8BA9;&#x6211;&#x4EEC;&#x80FD;&#x591F;&#x5B9E;&#x73B0;&#x4E00;&#x4E9B;&#x5DE5;&#x5177;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x66F4;&#x597D;&#x5730;&#x4E86;&#x89E3;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x3002;</p>
</li>
</ol>
<h3 id="&#x6700;&#x7B80;&#x5355;&#x7684;-store">&#x6700;&#x7B80;&#x5355;&#x7684; Store</h3>
<blockquote>
<p><strong>&#x63D0;&#x793A;&#xFF1A;</strong>&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x540E;&#x7EED;&#x7684;&#x6587;&#x6863;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x4E2D;&#x4F7F;&#x7528; ES2015 &#x8BED;&#x6CD5;&#x3002;&#x5982;&#x679C;&#x4F60;&#x8FD8;&#x6CA1;&#x80FD;&#x638C;&#x63E1; ES2015&#xFF0C;<a href="https://babeljs.io/docs/learn-es2015/" target="_blank">&#x4F60;&#x5F97;&#x6293;&#x7D27;&#x4E86;</a>&#xFF01;</p>
</blockquote>
<p><a href="installation.html">&#x5B89;&#x88C5;</a> Vuex &#x4E4B;&#x540E;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; store&#x3002;&#x521B;&#x5EFA;&#x8FC7;&#x7A0B;&#x76F4;&#x622A;&#x4E86;&#x5F53;&#x2014;&#x2014;&#x4EC5;&#x9700;&#x8981;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x521D;&#x59CB; state &#x5BF9;&#x8C61;&#x548C;&#x4E00;&#x4E9B; mutations&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x5982;&#x679C;&#x5728;&#x6A21;&#x5757;&#x5316;&#x6784;&#x5EFA;&#x7CFB;&#x7EDF;&#x4E2D;&#xFF0C;&#x8BF7;&#x786E;&#x4FDD;&#x5728;&#x5F00;&#x5934;&#x8C03;&#x7528;&#x4E86; Vue.use(Vuex)</span>

<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> Vuex.Store({
  state: {
    count: <span class="hljs-number">0</span>
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>store.state</code> &#x6765;&#x83B7;&#x53D6;&#x72B6;&#x6001;&#x5BF9;&#x8C61;&#xFF0C;&#x4EE5;&#x53CA;&#x901A;&#x8FC7; <code>store.commit</code> &#x65B9;&#x6CD5;&#x89E6;&#x53D1;&#x72B6;&#x6001;&#x53D8;&#x66F4;&#xFF1A;</p>
<pre><code class="lang-js">store.commit(<span class="hljs-string">&apos;increment&apos;</span>)

<span class="hljs-built_in">console</span>.log(store.state.count) <span class="hljs-comment">// -&gt; 1</span>
</code></pre>
<p>&#x518D;&#x6B21;&#x5F3A;&#x8C03;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x63D0;&#x4EA4; mutation &#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x800C;&#x975E;&#x76F4;&#x63A5;&#x6539;&#x53D8; <code>store.state.count</code>&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x66F4;&#x660E;&#x786E;&#x5730;&#x8FFD;&#x8E2A;&#x5230;&#x72B6;&#x6001;&#x7684;&#x53D8;&#x5316;&#x3002;&#x8FD9;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x7EA6;&#x5B9A;&#x80FD;&#x591F;&#x8BA9;&#x4F60;&#x7684;&#x610F;&#x56FE;&#x66F4;&#x52A0;&#x660E;&#x663E;&#xFF0C;&#x8FD9;&#x6837;&#x4F60;&#x5728;&#x9605;&#x8BFB;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;&#x80FD;&#x66F4;&#x5BB9;&#x6613;&#x5730;&#x89E3;&#x8BFB;&#x5E94;&#x7528;&#x5185;&#x90E8;&#x7684;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x8FD9;&#x6837;&#x4E5F;&#x8BA9;&#x6211;&#x4EEC;&#x6709;&#x673A;&#x4F1A;&#x53BB;&#x5B9E;&#x73B0;&#x4E00;&#x4E9B;&#x80FD;&#x8BB0;&#x5F55;&#x6BCF;&#x6B21;&#x72B6;&#x6001;&#x6539;&#x53D8;&#xFF0C;&#x4FDD;&#x5B58;&#x72B6;&#x6001;&#x5FEB;&#x7167;&#x7684;&#x8C03;&#x8BD5;&#x5DE5;&#x5177;&#x3002;&#x6709;&#x4E86;&#x5B83;&#xFF0C;&#x6211;&#x4EEC;&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x5982;&#x65F6;&#x95F4;&#x7A7F;&#x68AD;&#x822C;&#x7684;&#x8C03;&#x8BD5;&#x4F53;&#x9A8C;&#x3002;</p>
<p>&#x7531;&#x4E8E; store &#x4E2D;&#x7684;&#x72B6;&#x6001;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#xFF0C;&#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#x8C03;&#x7528; store &#x4E2D;&#x7684;&#x72B6;&#x6001;&#x7B80;&#x5355;&#x5230;&#x4EC5;&#x9700;&#x8981;&#x5728;&#x8BA1;&#x7B97;&#x5C5E;&#x6027;&#x4E2D;&#x8FD4;&#x56DE;&#x5373;&#x53EF;&#x3002;&#x89E6;&#x53D1;&#x53D8;&#x5316;&#x4E5F;&#x4EC5;&#x4EC5;&#x662F;&#x5728;&#x7EC4;&#x4EF6;&#x7684; methods &#x4E2D;&#x63D0;&#x4EA4; mutations&#x3002;</p>
<p>&#x8FD9;&#x662F;&#x4E00;&#x4E2A;<a href="https://jsfiddle.net/n9jmu5v7/341/" target="_blank">&#x6700;&#x57FA;&#x672C;&#x7684; Vuex &#x8BB0;&#x6570;&#x5E94;&#x7528;</a>&#x793A;&#x4F8B;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x66F4;&#x6DF1;&#x5165;&#x5730;&#x63A2;&#x8BA8;&#x4E00;&#x4E9B;&#x6838;&#x5FC3;&#x6982;&#x5FF5;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x4ECE; <a href="state.html">State</a> &#x6982;&#x5FF5;&#x5F00;&#x59CB;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="intro.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Vuex 是什么？">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"开始","level":"1.6","depth":1,"next":{"title":"核心概念","level":"1.7","depth":1,"ref":"","articles":[{"title":"State","level":"1.7.1","depth":2,"path":"state.md","ref":"state.md","articles":[]},{"title":"Getters","level":"1.7.2","depth":2,"path":"getters.md","ref":"getters.md","articles":[]},{"title":"Mutations","level":"1.7.3","depth":2,"path":"mutations.md","ref":"mutations.md","articles":[]},{"title":"Actions","level":"1.7.4","depth":2,"path":"actions.md","ref":"actions.md","articles":[]},{"title":"Modules","level":"1.7.5","depth":2,"path":"modules.md","ref":"modules.md","articles":[]}]},"previous":{"title":"Vuex 是什么？","level":"1.5","depth":1,"path":"intro.md","ref":"intro.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"root":"./","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"lanyue","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Gitbook demo","language":"zh-hans","gitbook":">=3.0.0","description":"this is a test"},"file":{"path":"getting-started.md","mtime":"2017-02-21T09:41:56.408Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-06T15:58:45.473Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

